package com.hxd.otc.module.wallet.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.dialog.PhotoDialog;
import com.hxd.otc.event.Event;
import com.hxd.otc.module.wallet.adapter.TransferCoinAdapter;
import com.hxd.otc.module.wallet.bean.WalletCurrencyBean;
import com.hxd.otc.module.wallet.contract.OtcToNormalContact;
import com.hxd.otc.module.wallet.presenter.OtcToNotmalAccountPresenter;
import com.hxd.otc.utils.StringUtil;
import com.hxd.otc.widget.LimitNumberTextWatcher;
import com.hxd.otc.widget.TitleBar;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;


public class OtcToNormalAccountActivity extends BaseActivity<OtcToNotmalAccountPresenter> implements OtcToNormalContact.View {

    @BindView(R.id.title_bar)
    TitleBar title_bar;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.tv_otc)
    TextView tvOtc;

    @BindView(R.id.tv_normal)
    TextView tvNormal;

    @BindView(R.id.tv_sure)
    TextView tvSure;

    @BindView(R.id.et_available_balance)
    EditText etAvailableBalance;
    // // 0：比比，1：C2C，2 保证金
    int from = 3;// 0：常规账户 ；1 法币交易账户 ； 2： 手续费备用金账户 3: UES 交易账户
    int to = 1;

    ArrayList<WalletCurrencyBean> otcInfo;
    //    ArrayList<WalletCurrencyBean> normalInfo = new ArrayList<>();
    int otcPos;
    boolean isClickFristButton = true;

    TransferCoinAdapter coinAdapter;
    double uesNumber;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_otc_to_normal_account;
    }

    @Override
    protected void initView() {

        otcInfo = (ArrayList<WalletCurrencyBean>) getIntent().getExtras().getSerializable("walletInfo");
//        normalInfo = (ArrayList<WalletCurrencyBean>) getIntent().getExtras().getSerializable("normalWalletInfo");
        otcPos = getIntent().getExtras().getInt("pos");
//        if (normalInfo == null || normalInfo.size() <= 0) {
//            return;
//        }
        etAvailableBalance.addTextChangedListener(new LimitNumberTextWatcher(etAvailableBalance, 9, 8));
//        for (int i = 0; i < normalInfo.size(); i++) {
//            if (normalInfo.get(i).getName().equals(otcInfo.get(otcPos).getName())) {
//                etAvailableBalance.setHint(getString(R.string.available_balance,
//                        normalInfo.get(i).volume + ""));
//                break;
//            }
//        }

        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        initAdapter();
    }

    private void initAdapter() {
        coinAdapter = new TransferCoinAdapter();
        otcInfo.get(otcPos).isCheck = true;
        coinAdapter.setNewData(otcInfo);
        coinAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                otcPos = position;
                for (int i = 0; i < otcInfo.size(); i++) {
                    otcInfo.get(i).isCheck = false;
                }
                otcInfo.get(otcPos).isCheck = true;
                coinAdapter.notifyDataSetChanged();
                getSymbolNumber();

            }
        });
        recyclerView.setAdapter(coinAdapter);
    }

    private void getSymbolNumber() {
        if (tvOtc.getText().equals(getString(R.string.ues_exchange))) {
            mPresenter.getAssetBySymbol(otcInfo.get(otcPos).getSymbol());
        } else {
            refreshCoinNumber(true);
        }
    }

    private void refreshCoinNumber(boolean isHint) {
        if (tvOtc.getText().toString().equals(getString(R.string.otc_account))) {//法币账户
            if (isHint) {
                etAvailableBalance.setText("");
                etAvailableBalance.setHint(getString(R.string.available_balance,
                        otcInfo.get(otcPos).volume + ""));
            } else {
                etAvailableBalance.setText(otcInfo.get(otcPos).volume + "");
            }
        } else if (tvOtc.getText().toString().equals(getString(R.string.normal_account))) {//常规账户
//            if (isHint) {
//                etAvailableBalance.setText("");
//                if (normalInfo == null) {
//                    return;
//                }
//                for (int i = 0; i < normalInfo.size(); i++) {
//                    if (normalInfo.get(i).getName().equals(otcInfo.get(otcPos).getName())) {
//                        etAvailableBalance.setHint(getString(R.string.available_balance,
//                                normalInfo.get(i).volume + ""));
//                        break;
//                    }
//                }
//            } else {
//                if (normalInfo == null) {
//                    return;
//                }
//                for (int i = 0; i < normalInfo.size(); i++) {
//                    if (normalInfo.get(i).getName().equals(otcInfo.get(otcPos).getName())) {
//                        etAvailableBalance.setText(normalInfo.get(i).volume + "");
//                        break;
//                    }
//                }
//            }

        } else if (tvOtc.getText().toString().equals(getString(R.string.fee_reserve))) {
            if (isHint) {
                etAvailableBalance.setText("");
                etAvailableBalance.setHint(getString(R.string.available_balance,
                        otcInfo.get(otcPos).bailVolume + ""));
            } else {
                etAvailableBalance.setText(otcInfo.get(otcPos).bailVolume + "");
            }
        } else if (tvOtc.getText().toString().equals(getString(R.string.ues_exchange))) {
            if (isHint) {
                etAvailableBalance.setText("");
                etAvailableBalance.setHint(getString(R.string.available_balance,
                        StringUtil.format8Number(uesNumber)));
            } else {
                etAvailableBalance.setText(StringUtil.handleZero(uesNumber + ""));
            }
        }
    }

    @Override
    protected void initData() {
        getSymbolNumber();
    }

    @Override
    protected void initEvent() {
        title_bar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
            @Override
            public void onBack(View v) {

            }

            @Override
            public void onRight(View v) {

                $startActivity(TransferRecordActivity.class, false);
            }
        });
    }

    @OnClick({R.id.tv_sure, R.id.rl_otc, R.id.rl_normal, R.id.tv_all_transer})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_sure:
                if (TextUtils.isEmpty(etAvailableBalance.getText().toString())) {
                    showToast(getString(R.string.enter_inout_number));
                    return;
                }
                if (from == 1 && to == 3) {
                    showToast(getString(R.string.only_support_ues_to_normal));
                    return;
                }
                if ((from == 2 && to == 3) || (from == 3 && to == 2)) {// 0：常规账户 ；1 法币交易账户 ； 2： 手续费备用金账户 3: UES 交易账户
                    showToast(getString(R.string.not_support_ues_to_fee));
                    return;
                }
                if (from == to) {
                    showToast(getString(R.string.not_support_same));
                    return;
                }

                if (tvOtc.getText().toString().equals(getString(R.string.fee_reserve)) && otcInfo.get(otcPos).bailVolume <= 0) {
                    showToast(getString(R.string.assest_must_bigger_than_zero));
                    return;
                }

                if (coinAdapter.getItem(otcPos) != null && otcInfo != null && otcInfo.get(otcPos) != null) {
                    // 0：比比，1：C2C，2 保证金
                    tvSure.setEnabled(false);
                    showSpinnerDlg("", false);
                    mPresenter.otcToNormalAccount(otcInfo.get(otcPos).getId(),
                            coinAdapter.getItem(otcPos).getName(),
                            etAvailableBalance.getText().toString(), from + "", to + "");
                }
                break;
            case R.id.rl_otc:
                isClickFristButton = true;
                showDialog();
                break;
            case R.id.rl_normal:
                isClickFristButton = false;
                showDialog();
                break;
            case R.id.tv_all_transer:
                refreshCoinNumber(false);
                break;

        }
    }

    PhotoDialog mPhotoDialog;

    public void showDialog() {
//        if (normalInfo == null || normalInfo.size() <= 0) {
//            return;
//        }
        if (mPhotoDialog == null) {
            mPhotoDialog = new PhotoDialog(this);
            mPhotoDialog.setWalletComing(true);
        }
        if (!mPhotoDialog.isShowing()) {
            mPhotoDialog.show();
        }

        mPhotoDialog.setOnPhotoDialogItemClickListener(new PhotoDialog.OnPhotoDialogItemClickListener() {
            @Override
            public void onClickCamera(View view) {
                mPhotoDialog.dismiss();
                etAvailableBalance.setText("");
                if (isClickFristButton) {
                    tvOtc.setText(R.string.otc_account);
                    etAvailableBalance.setHint(getString(R.string.available_balance,
                            otcInfo.get(otcPos).volume + ""));
                    from = 1;
                } else {
                    to = 1;
                    tvNormal.setText(R.string.otc_account);
                }

            }

            @Override
            public void onClickAlbum(View view) {
                mPhotoDialog.dismiss();
                etAvailableBalance.setText("");
                if (isClickFristButton) {
                    tvOtc.setText(R.string.ues_exchange);
//                    for (int i = 0; i < normalInfo.size(); i++) {
//                        if (normalInfo.get(i).getName().equals(otcInfo.get(otcPos).getName())) {
//                            etAvailableBalance.setHint(getString(R.string.available_balance,
//                                    normalInfo.get(i).volume + ""));
//                            break;
//                        }
//                    }
                    etAvailableBalance.setHint(getString(R.string.available_balance,
                            StringUtil.handleZero(uesNumber + "")));
                    from = 3;
                } else {
                    to = 3;
//                    tvNormal.setText(R.string.normal_account);
                    tvNormal.setText(R.string.ues_exchange);
                }

            }

            @Override
            public void onClickFee(View view) {
                mPhotoDialog.dismiss();
                etAvailableBalance.setText("");
                if (isClickFristButton) {
                    tvOtc.setText(R.string.fee_reserve);
                    etAvailableBalance.setHint(getString(R.string.available_balance,
                            otcInfo.get(otcPos).bailVolume + ""));
                    from = 2;
                } else {
                    tvNormal.setText(R.string.fee_reserve);
                    to = 2;
                }

            }

            @Override
            public void onClickCancel(View view) {
                mPhotoDialog.dismiss();
            }
        });
    }

    @Override
    protected OtcToNotmalAccountPresenter createPresenter() {
        return new OtcToNotmalAccountPresenter(this);
    }

    @Override
    public void onSuccess() {
        tvSure.setEnabled(true);
        dismissSpinnerDlg();
        post(new Event.NormalAccountEvent());
//        showToast(getString(R.string.successful_transfer));
        setResult(RESULT_OK);
        $startActivity(TransferRecordActivity.class, true);
    }

    @Override
    public void onFail() {
        dismissSpinnerDlg();
        setResult(RESULT_CANCELED);
        finish();
    }

    @Override
    public void getAssetBySymbolSucced(Double number) {
        uesNumber = number;
        refreshCoinNumber(true);
    }

    @Override
    public void getAssetBySymbolFail(String message) {
        showToast(message);

    }

}
